package net.caixiaomi.info.life;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.sobot.chat.utils.ScreenUtils;
import com.sobot.chat.utils.VersionUtils;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.util.LLog;

import java.math.BigDecimal;


public class UpdateDialog extends BaseDialog implements View.OnClickListener{
    private DialogClickListener dialogClickListener;
    private String mContent;
    private String mTitle;
    private boolean oneButton;
    private ProgressBar progressBar;
    private TextView tv_percentage_text;
    private boolean isSetMax;

    private Context mContext;
    private TextView mUpdateDes;

    public UpdateDialog(@NonNull Context context) {
        super(context);
        mContext=context;
    }
    public void setDialogClickListener(DialogClickListener dialogClickListener){
        this.dialogClickListener = dialogClickListener;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update_layout);
        setCanceledOnTouchOutside(false);
        setGravity(Gravity.CENTER,23,0);

        ImageView leftBt = findViewById(R.id.bt_cancel);
        progressBar = findViewById(R.id.progress_update);
        tv_percentage_text = findViewById(R.id.tv_percentage_text);
        mUpdateDes=findViewById(R.id.update_des);

        TextView logTv=findViewById(R.id.update_log);
        logTv.setText(mContent);
//        if(oneButton){
//            leftBt.setVisibility(View.GONE);
//        }
        if(oneButton){
            findViewById(R.id.update_back_ground_divider).setVisibility(View.GONE);
            findViewById(R.id.bt_cancel).setVisibility(View.GONE);
        }else{
            findViewById(R.id.update_back_ground_divider).setVisibility(View.VISIBLE);
            findViewById(R.id.bt_cancel).setVisibility(View.VISIBLE);
        }

        leftBt.setOnClickListener(this);
        findViewById(R.id.bt_update).setOnClickListener(this);

        RelativeLayout layout=findViewById(R.id.update_back_ground);
        RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) layout.getLayoutParams();
        int width= CommonApp.mScreenWidth- ScreenUtils.dip2px(mContext,40);
        int height=646*width/570;
        params.height=height;
        layout.setLayoutParams(params);

        TextView lTitleTv=findViewById(R.id.update_title_tv);
        lTitleTv.setText("发现新版本\nv "+ mTitle);
//        LinearLayout.LayoutParams tvParams= (LinearLayout.LayoutParams) lTitleTv.getLayoutParams();
        RelativeLayout lTitleGroup=findViewById(R.id.update_title_group);
        RelativeLayout.LayoutParams lTitleParams= (RelativeLayout.LayoutParams) lTitleGroup.getLayoutParams();
        lTitleParams.height=2*height/5;
        lTitleGroup.setLayoutParams(lTitleParams);

        RelativeLayout mScroll=findViewById(R.id.update_txt_group);
        ScrollView.LayoutParams scrolls= (ScrollView.LayoutParams) mScroll.getLayoutParams();
        scrolls.height=3*height/5-ScreenUtils.dip2px(mContext,90);
        mScroll.setLayoutParams(scrolls);

        progressBar.setProgress(0);
    }

    public void setContent(String title,String content){
        mContent = content;
        mTitle=title;
    }

    public void setProgressBar(long progress,long count){
        if(!isSetMax) {
            isSetMax = true;
        }
        setPercentage(progress,count);
    }
    private void setPercentage(long progress,long count){
        int i = (int) (progress * 100 / count);
//        LLog.i("i = " + i + " progress = " + progress + " max = " + progressBar.getMax());
        String text = String.valueOf(i) + "%";
        tv_percentage_text.setText(text);
        double lSize=(double) progress/(1000*1000);
        double mSize=(double) count/(1000*1000);
        lSize=new BigDecimal(lSize).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        mSize=new BigDecimal(mSize).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        String vlSize=lSize+"";
        String vmSize=mSize+"";
        String []vlSizes=vlSize.split("\\.");

        if(vlSizes.length==2){
            if(vlSizes[1].length()==1){
                vlSize=vlSize+"0";
            }else if(vlSizes[1].length()==0){
                vlSize=vlSize+"00";
            }
        }else{
            vlSize=vlSize+"0.00";
        }

        String []vmSizes=vmSize.split("\\.");
        if(vmSizes.length==2){
            if(vmSizes[1].length()<2){
                vmSize=vmSize+"0";
            }else if(vmSizes[1].length()==0){
                vmSize=vmSize+"00";
            }
        }else{
            vmSize=vmSize+"0.00";
        }
        //123/1024M        60%

        mUpdateDes.setText(vlSize+"/"+vmSize+"M"+"        "+i+"%");
        progressBar.setProgress(i);
    }

    public void setOneButton(boolean hasOneButton){
        this.oneButton = hasOneButton;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_cancel:
                if(dialogClickListener != null){
                    dialogClickListener.onLeftBtnClick(v);
                }
                break;
            case R.id.bt_update:
                RelativeLayout updateProgress=findViewById(R.id.update_progress_group);
                updateProgress.setVisibility(View.VISIBLE);
                findViewById(R.id.bt_update).setVisibility(View.GONE);
                findViewById(R.id.update_scroll).setVisibility(View.GONE);
                if(dialogClickListener != null){
                    dialogClickListener.onRightBtnClick(v);
                }
                break;
        }
    }
}
